<template>
  <div class="app-container">
    <el-card class="box-card search-div">
        <el-form :inline="true">
        <el-form-item label="名称">
            <el-input placeholder="请输入查询的名称" v-model="listQuery.moduleName"/>
        </el-form-item>
        <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
        <el-button @click="handleReset" icon="el-icon-refresh">重置</el-button>
        </el-form>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" v-hasPermi="['sys_platform_add']" class="el-icon-plus" @click="add">新增</el-button>
      </div>
        <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row class="table-list">
          <el-table-column label="序号" type="index" min-width="5%"/>
          <el-table-column align="center" label="名称" prop="moduleName">
          </el-table-column>
          <el-table-column align="center" label="排序" prop="orderNum">
          </el-table-column>
          <el-table-column align="center" label="图标" prop="icon">
            <template slot-scope="scope">
            <i :class="'iconfont '+scope.row.icon"></i>
          </template>
          </el-table-column>
          <el-table-column align="center" label="备注" prop="remark">
          </el-table-column>
          <el-table-column align="center" label="更新时间" prop="updateTime" width="150px">
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button plain v-hasPermi="['sys_platform_edit']" type="violet" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button plain v-hasPermi="['sys_platform_remove']" type="danger" @click="removeByIds(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination-container">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
    </el-card>
        <!--弹出框-->
        <el-dialog :title="dialogTitle" :width="'600px'" :visible.sync="showDialog" :close-on-click-modal="false">
          <el-form :rules="rules" ref="saveSysPlatformData" :model="saveSysPlatformData" label-width="100px">
            <el-form-item label="名称:" prop="moduleName">
              <el-input v-model="saveSysPlatformData.moduleName" :maxlength="20"  placeholder="最多输入20个字符"></el-input>
            </el-form-item>
            <el-form-item label="图标:" prop="icon">
              <el-input v-model="saveSysPlatformData.icon" :maxlength="50"  placeholder="最多输入50个字符"></el-input>
            </el-form-item>
            <el-form-item label="排序:" prop="orderNum">
              <el-input-number v-model="saveSysPlatformData.orderNum" :min="0" :max="999999" style="width: 100%;"></el-input-number>
            </el-form-item>
            <el-form-item label="备注:" prop="remark">
              <el-input v-model="saveSysPlatformData.remark" type="textarea" :rows="2" :maxlength="100" placeholder="最多输入100个字符"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="showDialog = false">取 消</el-button>
            <el-button type="primary" @click="onSave('saveSysPlatformData')">确 定</el-button>
          </div>
        </el-dialog>


  </div>
</template>

<script>
import * as sysPlatform from './js/sys'
import { mapGetters } from 'vuex'

export default {
  name: 'sysPlatformList',
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1, limit: 10, moduleName: ''
      },
      showDialog: false,
      saveSysPlatformData: {
        id: '',
        moduleName: '',
        orderNum: '',
        icon: '',
        remark: '',
      },
      rules: {
        moduleName: [{ required: true, message: '请输入顶部菜单名称', trigger: 'blur' }]
      },
      dialogTitle: '添加顶部菜单'
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created() {
    this.getList()
  },
  methods: {
    getList() { // 获取所有顶部菜单
      this.listLoading = true
      sysPlatform.fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleReset(){
      this.listQuery = {
        page: 1, limit: 10, moduleName: ''
      }
      this.getList()
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    //删除
    removeByIds(id) {
      this.$confirm('请确定是否删除该菜单?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        sysPlatform.removeByIds(id, status).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        })
      }).catch(function(error) { console.log(error) })
    },
    // 新增
    add(pit, index) {
      this.showDialog = true
      this.dialogTitle = '添加顶部菜单'
      // 清空表单数据
      this.$nextTick(() => {
        this.resetForm('saveSysPlatformData')
      })
    },
    // 编辑
    handleEdit(index, row) {
      this.showDialog = true
      this.dialogTitle = '编辑顶部菜单'
      this.$nextTick(() => {
        this.resetForm('saveSysPlatformData')
        this.saveSysPlatformData.id = row.id
        this.saveSysPlatformData.moduleName = row.moduleName
        this.saveSysPlatformData.orderNum = row.orderNum
        this.saveSysPlatformData.icon = row.icon
        this.saveSysPlatformData.remark = row.remark
      })
    },
    // 重置表单 解决验证提示的问题
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 保存
    onSave(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.showDialog = false
          sysPlatform.save(this.saveSysPlatformData).then((response) => {
            this.$message({ type: 'success', message: '操作成功!' })
            this.getList()
          }).catch(function(error) {
            console.log(error)
          })
        }
      })
    }
  }
}
</script>
